#include<stdio.h>
#include<stdlib.h>
#include<string.h>


struct LinkNode{
    int num;
    struct LinkNode * next;
};

// 初始化
struct LinkNode * initLinkList(){
    //创建头结点
    struct LinkNode *pHeader = malloc(sizeof(struct LinkNode));
    if(pHeader ==NULL){
        return NULL;
    }

    //初始化头结点
    // 头结点不维护数据域
    pHeader->next = NULL;

    // 记录为节点位置，方便插入新的数据
    struct LinkNode * pTail = pHeader;

    int val = -1;
    while(1){
        scanf("%d", &val);

        if(val == -1){
            break;
        }

        // 如果不是-1，插入节点到链表中
        struct LinkNode * newNode = malloc(sizeof(struct LinkNode));
        newNode->num = val;
        newNode->next = NULL;

        // 更改指针的指向
        pTail->next = newNode;
        pTail = pTail->next;
    }

    return pHeader;
}

// 遍历
void foreach_list(struct LinkNode * pHeader){
    if(pHeader == NULL){
        return;
    }

    struct LinkNode * pCurrent = pHeader->next;
    while(pCurrent!=NULL){
        printf("num:%d\n", pCurrent->num);
        pCurrent = pCurrent->next;
    }

}

// 插入链表
void Insert(struct LinkNode * pHeader,int old, int new){
    if(pHeader == NULL){
        return;
    }

     //创建两个临时节点
    struct LinkNode * pPrev = pHeader;
    struct LinkNode * pCurrent = pHeader->next;

    while(pCurrent !=NULL){
        if(pCurrent->num == old){
            break;
        }

        //找到没对应的位置，
        pPrev = pCurrent;
        pCurrent = pCurrent->next;
    }

    // 创建新节点
    struct LinkNode * newNode = malloc(sizeof(struct LinkNode));
    newNode->num = new;
    newNode->next = NULL;

    // 建立关系
    newNode->next = pCurrent;
    pPrev->next = newNode;
    


}

void main(){
    struct LinkNode * p = initLinkList();

    foreach_list(p);


}